﻿using System;
using System.Collections.Generic;
using Rendiciones.Web.infraestructura;
using Rendiciones.Logica.Modelos;
using System.Globalization;
using System.Web.UI.WebControls;

namespace Rendiciones.Web.usercontrols
{
    public partial class ctrlWorkFlow : UserControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Mostrar(int idRendicion)
        {
            LimpiarErrores(pnlWf);
            pnlWf.Attributes.Remove("style");
            pnlWf.Attributes.Add("style", "width:700px;");
            Cargar(idRendicion);
            mpeWf.Show();
        }

        public void Ocultar()
        {
            mpeWf.Hide();
        }

        private void Cargar(int idRendicion)
        {
            try
            {
                var workflows = Modulos.Rendiciones.WorkFlows.Obtener(idRendicion);
                rptWorkFlow1.DataSource = workflows;
                rptWorkFlow2.DataSource = workflows;
                rptWorkFlow1.DataBind();
                rptWorkFlow2.DataBind();
                Ver(0);
            }
            catch(Exception exception)
            {
                MuestraMensajeError(pnlWf, "<strong>Error:</strong> " + exception.Message);
            }
        }

        protected void RptWorkFlowItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var index = int.Parse(e.CommandArgument.ToString());
            Ver(index);
        }

        private void Ver(int index)
        {
            foreach (RepeaterItem item in rptWorkFlow1.Items)
                ((Button)item.FindControl("btnEtapa")).CssClass = "boton w100p";

            foreach (RepeaterItem item in rptWorkFlow2.Items)
                item.FindControl("divEtapa").Visible = false;

            ((Button)rptWorkFlow1.Items[index].FindControl("btnEtapa")).CssClass = "boton select w100p";
            rptWorkFlow2.Items[index].FindControl("divEtapa").Visible = true;

            lblProyecto.Text = ((HiddenField)rptWorkFlow2.Items[index].FindControl("hdfProyecto")).Value;
            lblCustodio.Text = ((HiddenField)rptWorkFlow2.Items[index].FindControl("hdfCustodio")).Value;
        }

        protected void BtnCerrarWfClick(object sender, EventArgs e) { Ocultar(); }
        protected void BtnCerrarClick(object sender, EventArgs e) { Ocultar(); }
    }
}